/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis;

import com.cxhd.lis.device.US_2000;
import com.cxhd.lis.util.DateUtil;
import com.cxhd.lis.util.FileUtil;
import com.cxhd.lis.util.Tool;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;

/**
 *
 * @author 52461
 */
public class Test {

    public static void main(String[] args) throws FileNotFoundException, IOException {
       
        String baseData = "<STX> Date:2017-06-09 18:03   <CR><LF> Operator: 01<CR><LF> No.  000002<CR><LF> LEU       -              neg<CR><LF> NIT       -              neg<CR><LF> URO       -       3.5 umol/L<CR><LF> PRO       -              neg<CR><LF> pH          6.5<CR><LF> BLO       -              neg<CR><LF> SG        1.005<CR><LF> KET       -              neg<CR><LF> BIL       -              neg<CR><LF> GLU       -              neg<CR><LF>*ASC      3+       2.28mmol/L<CR><LF><ETX>";
       
        String sampleNo = null;
        String testDate = null;
        String testTime = null;
          String baseLine = System.getProperty("line.separator");
       
        String strs[] = baseData.split("\n");
        Map<String, String> map = new HashMap<String, String>();
        List<String> list = new ArrayList<String>();
        String key = "";
     
        for (int i=0;i<strs.length;i++) {
            System.out.println(i+"  "+strs[i]);
            if(i==2){
            System.out.println(strs[i].split(":")[1]);
            }
            if(i>2){
              System.out.println(strs[i].split("=")[0]+"   ===== "+strs[i].split("=")[1]);
            }
//            if (s.trim().length() > 0) {
//                list.add(s);
//            }

        }

       
        }

    

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    public static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public void saveToImgFile(String src, String output) {
        if (src == null || src.length() == 0) {
            return;
        }
        try {
            FileOutputStream out = new FileOutputStream(new File(output));
            byte[] bytes = src.getBytes();
            for (int i = 0; i < bytes.length; i += 2) {
                out.write(charToInt(bytes[i]) * 16 + charToInt(bytes[i + 1]));
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private int charToInt(byte ch) {
        int val = 0;
        if (ch >= 0x30 && ch <= 0x39) {
            val = ch - 0x30;
        } else if (ch >= 0x41 && ch <= 0x46) {
            val = ch - 0x41 + 10;
        }
        return val;
    }

    public static void parseComData(String baseData) {

        try {
            String sampleNo = null;
            String testDate = DateUtil.getDateEN();

            String testTime = null;
            Map<String, String> resultMap = new HashMap<String, String>();

            String alls[] = baseData.split("\n");
            System.out.println(alls.length);
            int n = 0;
            for (int i = 0; i < alls.length; i++) {
                // System.out.println(i+":"+alls[i]);
                //第一行不做处理
                //第4行为 样本号和日期
                if (alls[i].indexOf("NO.") > -1) {
                    System.out.println(alls[i].replaceAll(" +", "@"));
                    n = i;
                    sampleNo = alls[i].replaceAll(" +", "@").split("@")[1].replace("NO.", "");

                    System.out.println(sampleNo + "@@@@@@@@@@@@@@@@@");

                }//第三行之后为结果
                else if (n != 0 && i > n) {
                    System.out.println(i + "i++++++++++++++++++");
                    parseResult(alls[i], resultMap);
                }
            }
            // sendDb(Tool.getSampleNo(code, sampleNo), testDate, resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据不完整解析错误");
        }

    }

    public static void parseResult(String data, Map<String, String> map) {
        System.out.println(data);
        String das[] = data.trim().replaceAll(" +", "@").split("@");

        if (das.length > 1) {
            String result = "";
            for (int n = 0; n < das.length; n++) {
                //System.out.println(das[0]+"="+das[1]);
            }
            // map.put(das[0], getResult(result));

        }
    }
}
